//
//  InputTempletTwoTableViewCell.m
//  CedarFinancePH
//
//  Created by menchao on 2018/3/29.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "InputTempletTwoTableViewCell.h"

@interface InputTempletTwoTableViewCell ()


@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIView *rightView;
@property (strong, nonatomic) UIView *lineView;
@property (strong, nonatomic) UIImageView *errImagV;
@property (strong, nonatomic) UILabel *errorLabel;
@end

@implementation InputTempletTwoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self!=nil){
        self.inputMaxLength = 0;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.inputTextField];
        [self.contentView addSubview:self.lineView];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kRegisterMarginOffsetX);
            make.top.mas_equalTo(self.contentView).mas_offset(15);
        }];
        [self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(14);
            make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.top.mas_equalTo(self.inputTextField.mas_bottom).mas_offset(5);
            make.height.mas_equalTo(1);
            make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
        }];
    }
    return self;
}

- (void)setIsPassword:(BOOL)isPassword{
    [self.inputTextField setSecureTextEntry:isPassword];
}

- (void)setPlaceholder:(NSString *)placeholder{
    self.inputTextField.placeholder = placeholder;
    [UIConfigure configureUITextField:self.inputTextField fontSize:14 color:kGrayTextColor];
}

- (void)setCellTitle:(NSString *)cellTitle{
    self.titleLabel.text = cellTitle;
}

- (void)setRightView:(UIView *)rightView size:(CGSize)size{
    if (_rightView) {
        [_rightView removeFromSuperview];
    }
    _rightView = rightView;
    
    if (self.showError) {
        [self.contentView addSubview:self.errImagV];
        [self.errImagV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kRegisterMarginOffsetX);
            make.top.mas_equalTo(self.contentView).mas_offset(10);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
        
        [self.contentView addSubview:self.errorLabel];
        [self.errorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.errImagV.mas_right).mas_offset(kMarginOffsetX );
            make.centerY.mas_equalTo(self.errImagV);
        }];
    }else{
        [self.errImagV removeFromSuperview];
        [self.errorLabel removeFromSuperview];
    }
    
    if(rightView){
        [self.contentView addSubview:_rightView];
        [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kRegisterMarginOffsetX);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(14);
            make.right.mas_equalTo(_rightView.mas_left).mas_offset(-10);
        }];
        [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
            make.centerY.mas_equalTo(self.inputTextField).mas_offset(-5);
            make.size.mas_offset(size);
        }];
    }else{
        [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(14);
            make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
        }];
    }
}

- (void)setRightView:(UIView *)rightView size:(CGSize)size marginY:(CGFloat)marginY{
    if (_rightView) {
        [_rightView removeFromSuperview];
    }
    _rightView = rightView;
    if(rightView){
        
        [self.contentView addSubview:_rightView];
        [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).mas_offset(kRegisterMarginOffsetX);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(14);
            make.right.mas_equalTo(_rightView.mas_left).mas_offset(-10);
        }];
        [_rightView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
            make.centerY.mas_equalTo(self.inputTextField).mas_offset(-marginY);
            make.size.mas_offset(size);
        }];
    }else{
        [self.inputTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.titleLabel);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(14);
            make.right.mas_equalTo(self.contentView).mas_offset(-kRegisterMarginOffsetX);
        }];
    }
}






- (void)textFieldDidChange:(UITextField *)textField{
    if (self.inputMaxLength>0) {
        if (textField.text.length > self.inputMaxLength) {
            textField.text = [textField.text substringToIndex:self.inputMaxLength];
        }
    }
    if (self.textChangeBlock) {
        self.textChangeBlock(self.tag,textField.text);
    }
}


- (UITextField *)inputTextField{
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc]init];
        [_inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_inputTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        _inputTextField.font = PingFangZhongChangGuiForFont(15);
     }
    return _inputTextField;
}

MACRO_UILabel_LazyloadWithPFCGFont(titleLabel, @"", kInputTitleColor, 14)

MACRO_UIView_Lazyload(lineView, [UIColor colorWithHexString:@"0xe9e9e9"])

MACRO_UIImageView_Lazyload(errImagV,@"error")

MACRO_UILabel_LazyloadWithPFCGFont(errorLabel, @"手机号码已注册", [UIColor redColor], 14)

+ (CGFloat)heightForCell{
    return 73;
}

@end
